package com.example.demo.web;


import com.alibaba.fastjson.JSONObject;
import com.example.demo.auth.AuthService;
import com.example.demo.auth.PolicySetService;
import com.example.demo.util.HttpClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/policy")
public class PolicyController {

    @Autowired
    PolicySetService policySetService;

    @Autowired
    AuthService authService;

    @PostMapping("/auth")
    public JSONObject auth(@RequestBody String data){
        JSONObject jsonObject = JSONObject.parseObject(data);

//        Identity identity = new Identity();
//        identity.setIdentity_user(new Identity_User(jsonObject.getString("USER")));
//        identity.setIdentity_device(new Identity_Device(jsonObject.getString("MAC")));
//        JSONObject envObject = jsonObject.getJSONObject("ENV");
//        identity.setIdentity_env(new Identity_Env(envObject.getString("IP"), envObject.getString("OS"),
//                envObject.getLong("LAST_LOGIN_TIME"), envObject.getString("VERSION")));
        String serviceName = jsonObject.getString("SERVICE");

        return authService.auth(jsonObject,serviceName);
    }

    @PostMapping("/policy_set")
    public String policySet(@RequestBody String data){
        JSONObject jsonObject = JSONObject.parseObject(data);
        String roleName = jsonObject.getString("ROLE");
        int trust = jsonObject.getIntValue("TRUST");
        String serviceName = jsonObject.getString("SERVICE");

        policySetService.PolicySet(serviceName,roleName,trust);
        return "set success";
    }

    @PostMapping("/test2")
    public JSONObject test2(@RequestBody String data){
        String testStr = "hello nihao";
        String res = HttpClientUtil.doPostJson("http://localhost:5002/policy/test",testStr);
        return JSONObject.parseObject(res);

    }

}
